import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import store from './store'
import VueLazyLoad from 'vue-lazyload'
import VueCookie from 'vue-cookie'
import {Message} from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
// import env from './env'

const mock = true
if (mock) {
  // import是预编译加载 require是请求加载 只有mock开关打开才会加载
  require('./mock/api')
}
// 根据前端的跨域方式做调整
// 接口代理的跨域方式
// axios.defaults.baseURL = '/api'
axios.defaults.baseURL = '/api'


// 根据环境变量获取不同的请求地址
// axios.defaults.baseURL = env.baseURL

// 超时
axios.defaults.timeout = 8000
// 接口错误拦截
axios.interceptors.response.use(function(response) {
  let res = response.data; // 接口返回的值
  let path = location.hash;
  if (res.status == 0) {
    return res.data;
    // 拦截业务错误码
  } else if (res.status == 10) { // 未登录错误码
    if (path !== '#/index') {
      window.location.href = '/#/login'
      return Promise.reject(res)
    }
  } else {
    Message.error(res.msg)
    return Promise.reject(res)
  }
  // 拦截服务端500
}, (error) => {
  let res = error.response;
  console.log(res)
  Message.error(res.data.message)
});
Vue.use(VueAxios, axios)
Vue.use(VueLazyLoad, {
  loading: '/imgs/loading-svg/loading-bars.svg'
})
Vue.use(VueCookie)
Vue.use(Message)
Vue.prototype.$message = Message
Vue.config.productionTip = false
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
